﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace iStudio.Library
{
    public static class Text
    {
        public static string FrequencyToText (float value, bool appendHz = false)
        {
            int valueInt = (int)value;

            if (value > 1000)
            {
                valueInt = valueInt / 1000;

                return !appendHz ? valueInt + "K" : valueInt + " KHz";
            }

            return !appendHz ? valueInt.ToString(CultureInfo.InvariantCulture) : valueInt + " Hz";
        }

        /// <summary>
        /// Expects string of format: 100, 200, 200, .. and returns integer array
        /// </summary>
        /// <param name="text">string of upper format</param>
        /// <returns>array of ints</returns>
        public static int[] TextToIntArray (string text)
        {
            string[] values = text.Trim().Split (' ');

            List<int> ints = new List<int>();

            foreach (string t in values)
            {
                try
                {
                    ints.Add (Convert.ToInt32 (t.Trim()));
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
                // ReSharper restore EmptyGeneralCatchClause
                {
                    
                }
            }

            return ints.ToArray ();
        }

        public static double[] TextToDoubleArray (string text)
        {
            string[] values = text.Trim().Split(' ');

            List<double> doubles = new List<double>();

            foreach (string t in values)
            {
                try
                {
                    doubles.Add(Convert.ToDouble(t.Trim()));
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
                // ReSharper restore EmptyGeneralCatchClause
                {

                }
            }

            return doubles.ToArray();
        }

        public static float[] TextToFloatArray (string text)
        {
            string[] values = text.Trim().Split(' ');

            List<float> floats = new List<float>();

            foreach (string t in values)
            {
                try
                {
                    floats.Add(Convert.ToSingle(t.Trim()));
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
                // ReSharper restore EmptyGeneralCatchClause
                {

                }
            }

            return floats.ToArray();
        }
    }
}
